route.test.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /* @vitest-environment node */
  2. import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
  3. import fs from "node:fs/promises";
  4. import os from "node:os";
  5. import path from "node:path";
  6. vi.mock("@/lib/auth/session", () => ({
  7. getSession: vi.fn(),
  8. }));
  9. import { getSession } from "@/lib/auth/session";
  10. import { GET, dynamic } from "./route.js";
  11. describe("GET /api/branches/[branch]/years", () => {
  12. let tmpRoot;
  13. const originalNasRoot = process.env.NAS_ROOT_PATH;
  14. beforeEach(async () => {
  15. vi.clearAllMocks();
  16. tmpRoot = await fs.mkdtemp(path.join(os.tmpdir(), "api-years-"));
  17. process.env.NAS_ROOT_PATH = tmpRoot;
  18. // Minimal structure for NL01
  19. await fs.mkdir(path.join(tmpRoot, "NL01", "2024"), { recursive: true });
  20. });
  21. afterEach(async () => {
  22. process.env.NAS_ROOT_PATH = originalNasRoot;
  23. if (tmpRoot) await fs.rm(tmpRoot, { recursive: true, force: true });
  24. });
  25. it('exports dynamic="force-dynamic" (RHL-006)', () => {
  26. expect(dynamic).toBe("force-dynamic");
  27. });
  28. it("returns 401 when unauthenticated", async () => {
  29. getSession.mockResolvedValue(null);
  30. const res = await GET(
  31. new Request("http://localhost/api/branches/NL01/years"),
  32. { params: Promise.resolve({ branch: "NL01" }) }
  33. );
  34. expect(res.status).toBe(401);
  35. expect(await res.json()).toEqual({
  36. error: { message: "Unauthorized", code: "AUTH_UNAUTHENTICATED" },
  37. });
  38. });
  39. it("returns 403 when branch user accesses a different branch", async () => {
  40. getSession.mockResolvedValue({
  41. role: "branch",
  42. branchId: "NL01",
  43. userId: "u1",
  44. });
  45. const res = await GET(
  46. new Request("http://localhost/api/branches/NL02/years"),
  47. { params: Promise.resolve({ branch: "NL02" }) }
  48. );
  49. expect(res.status).toBe(403);
  50. expect(await res.json()).toEqual({
  51. error: { message: "Forbidden", code: "AUTH_FORBIDDEN_BRANCH" },
  52. });
  53. });
  54. it("returns years for a valid branch when allowed", async () => {
  55. getSession.mockResolvedValue({
  56. role: "branch",
  57. branchId: "NL01",
  58. userId: "u1",
  59. });
  60. const res = await GET(
  61. new Request("http://localhost/api/branches/NL01/years"),
  62. { params: Promise.resolve({ branch: "NL01" }) }
  63. );
  64. expect(res.status).toBe(200);
  65. expect(await res.json()).toEqual({ branch: "NL01", years: ["2024"] });
  66. });
  67. it("returns 400 when branch param is missing (authenticated)", async () => {
  68. getSession.mockResolvedValue({
  69. role: "admin",
  70. branchId: null,
  71. userId: "u2",
  72. });
  73. const res = await GET(new Request("http://localhost/api/branches//years"), {
  74. params: Promise.resolve({ branch: undefined }),
  75. });
  76. expect(res.status).toBe(400);
  77. expect(await res.json()).toEqual({
  78. error: {
  79. message: "Missing required route parameter(s)",
  80. code: "VALIDATION_MISSING_PARAM",
  81. details: { params: ["branch"] },
  82. },
  83. });
  84. });
  85. it("returns 404 when the branch folder does not exist (authorized)", async () => {
  86. getSession.mockResolvedValue({
  87. role: "admin",
  88. branchId: null,
  89. userId: "u2",
  90. });
  91. const res = await GET(
  92. new Request("http://localhost/api/branches/NL99/years"),
  93. { params: Promise.resolve({ branch: "NL99" }) }
  94. );
  95. expect(res.status).toBe(404);
  96. expect(await res.json()).toEqual({
  97. error: {
  98. message: "Not found",
  99. code: "FS_NOT_FOUND",
  100. details: { branch: "NL99" },
  101. },
  102. });
  103. });
  104. it("returns 500 when NAS_ROOT_PATH is invalid (authenticated)", async () => {
  105. getSession.mockResolvedValue({
  106. role: "admin",
  107. branchId: null,
  108. userId: "u2",
  109. });
  110. process.env.NAS_ROOT_PATH = path.join(tmpRoot, "does-not-exist");
  111. const res = await GET(
  112. new Request("http://localhost/api/branches/NL01/years"),
  113. { params: Promise.resolve({ branch: "NL01" }) }
  114. );
  115. expect(res.status).toBe(500);
  116. expect(await res.json()).toEqual({
  117. error: { message: "Internal server error", code: "FS_STORAGE_ERROR" },
  118. });
  119. });
  120. });